package com.gsoe.judge.controller;

import com.gsoe.base.model.MqMessage;
import com.gsoe.base.model.Result;
import com.gsoe.dto.SaveCacheDTO;
import com.gsoe.judge.service.ExamCacheService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 考试答题缓存 前端控制器
 * </p>
 *
 * @author xiaoma
 * @since 2024-11-04
 */
@RestController
@RequestMapping("/judge/examCache")
public class ExamCacheController {
    @Autowired
    private ExamCacheService examCacheService;


    @DeleteMapping("/delByExamId/{id}")
    public Result<String> delByExamId(@PathVariable("id") Long id){
        examCacheService.delByTextId(id);
        return Result.success("ok");
    }

    @GetMapping("/getExamCache/{examId}/{userId}")
    public Result<Map<Long,String>> getExamCache(@PathVariable("examId") Long examId,@PathVariable("userId") Long userId){
        return Result.success(examCacheService.getExamCache(examId,userId));
    }

    @PostMapping("/saveInput")
    public Result<String> saveInput(@RequestBody @Valid SaveCacheDTO saveCacheDTO) {
        boolean b = examCacheService.saveInput(saveCacheDTO);
        if(!b){
            Result<String> result = new Result<>();
            result.setCode(503);
            result.setMsg("测验已结束");
            return result;
        }
        return Result.success("ok");
    }

    @PostMapping("/getExamCacheByUserList")
    //1、examId
    //2、userList
    public Result<Map<Long,Map<Long,String>>> getExamCacheByUserList(@RequestBody MqMessage mqMessage){
        return Result.success(examCacheService.getExamCacheByUserList(mqMessage));
    }
}
